import { Controller } from "../../mvc/core/Controller";
import { HolidaySignHintBtn } from "../view/holidaySign/HolidaySignHintBtn";
import { IHolidaySignModule } from "../../../modules/interfaces/IHolidaySignModule";
import { IView } from "../../mvc/interfaces/IView";
import { HolidaySignModule } from "../../../modules/HolidaySignModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { ClockManager } from "../manager/ClockManager";
import { GameConst } from "../../component/gconst/GameConst";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { HolidaySignConfig } from "../resource/HolidaySignConfig";
import { HolidaySignData } from "../view/holidaySign/HolidaySignData";
import { TSignConfig } from "../../../Message/Db/Tables/TSignConfig";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ESignStatus } from "../../../Message/Public/ESignStatus";
import { EffectManager } from "../manager/EffectManager";
import { FilterConst } from "../../component/gconst/FilterConst";
type int = number;
//class HolidaySignController
    
    export  class HolidaySignController extends Controller
    {
       

        public get hintBtn(): HolidaySignHintBtn
        {
            if (!this._hintBtn) 
            {
                this._hintBtn = new HolidaySignHintBtn();
            }
            return this._hintBtn;
        }

        public get holidaySign(): IHolidaySignModule
        {
            if (null == this._holidaySignModule) 
            {
                this.initView();
            }
            return this._holidaySignModule;
        }

        public /* override */ get view(): IView
        {
            return this.initView();
        }

        protected /* override */ initView(): IView
        {
            if (null == this._holidaySignModule) 
            {
                this._holidaySignModule = new HolidaySignModule();
            }
            return this._holidaySignModule;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeHolidaySign, this.onActiveTypeHolidaySign);
            Dispatcher.addEventListener(EventName.HolidaySignGetStatus, this.onHolidaySignGetStatus);
            Dispatcher.addEventListener(EventName.HolidaySignGetStatusSucess, this.onHolidaySignGetStatusSucess);
            Dispatcher.addEventListener(EventName.HolidaySignGetGifBag, this.onHolidaySignGetGifBag);
            Dispatcher.addEventListener(EventName.HolidaySignGetGifBagSucess, this.onHolidaySignGetGifBagSucess);
            Dispatcher.addEventListener(EventName.HolidaySignOpenModule, this.onHolidaySignOpenModule);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdateHandler);
            return;
        }

        protected onRoleLevelUpdateHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=false;
            if (this._activeToClient) 
            {
                loc1 = cache.active.isActiveInEffect(EActiveType._EActiveTypeHolidaySign);
                if (loc1) 
                {
                    ClockManager.instance.addDateCall(this._activeToClient.endDt, this.removeActive);
                    if (cache.role.entityInfo.level >= GameConst.IllustateOpenLevel) 
                    {
                        this.hintBtn.show();
                        this.getStatus(false);
                    }
                    else if (this._hintBtn) 
                    {
                        this._hintBtn.hide();
                    }
                }
            }
            else if (this._hintBtn) 
            {
                this._hintBtn.hide();
            }
            return;
        }

        /* internal  */onHolidaySignOpenModule(arg1: DataEvent): void
        {
            this.holidaySign.show();
            return;
        }

        /* internal  */onHolidaySignGetGifBag(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.roleProxy.getHolidaySign(loc1);
            return;
        }

        /* internal  */onHolidaySignGetGifBagSucess(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1=arg1.data.num as int;
            var loc2=arg1.data.bagStatus as Map<any, any> /* flash.utils.Dictionary */;
            cache.role.holidaySignDic = loc2;
            this.checkBtnShine(loc2);
            if (!this.holidaySign.isHide) 
            {
                loc3 = this.getShowArr(loc2, loc1);
                this.holidaySign.updateStatus(loc3);
            }
            return;
        }

        /* internal  */onHolidaySignGetStatusSucess(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1=arg1.data.open as Boolean;
            var loc2=arg1.data.bagStatus as Map<any, any> /* flash.utils.Dictionary */;
            cache.role.holidaySignDic = loc2;
            this.checkBtnShine(loc2);
            if (loc1) 
            {
                loc3 = this.getShowArr(loc2);
                this.holidaySign.updateStatus(loc3);
                if (this._activeToClient) 
                {
                    this.holidaySign.updateActTimeTx(this._activeToClient);
                }
                this.holidaySign.show();
            }
            return;
        }

        /* internal  */getShowArr(arg1: Map<any, any> /* flash.utils.Dictionary */, arg2: int=-1): Array<any>
        {
            var loc4=null;
            var loc5=0;
            var loc1=[];
            var loc2;
            var loc3=(loc2 = HolidaySignConfig.instance.rewardArr).length;
            var loc6=0;
            while (loc6 < loc3) 
            {
                (loc4 = new HolidaySignData()).tHolidaySign = loc2[loc6];
                loc5 = (loc2[loc6] as TSignConfig).id;
                loc4.btnStatus = arg1[loc5];
                if (loc6 + 1 != arg2) 
                {
                    loc4.isFly = false;
                }
                else 
                {
                    loc4.isFly = true;
                }
                loc1.push(loc4);
                ++loc6;
            }
            return loc1;
        }

        /* internal  */onHolidaySignGetStatus(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            this.getStatus(loc1);
            return;
        }

        /* internal  */onActiveTypeHolidaySign(arg1: Object): void
        {
            this._activeToClient = arg1 as SActiveToClient;
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeHolidaySign);
            if (loc1) 
            {
                ClockManager.instance.addDateCall(this._activeToClient.endDt, this.removeActive);
                if (cache.role.entityInfo.level >= GameConst.IllustateOpenLevel) 
                {
                    this.hintBtn.show();
                    this.getStatus(false);
                }
                else if (this._hintBtn) 
                {
                    this._hintBtn.hide();
                }
            }
            else if (this._hintBtn) 
            {
                this._hintBtn.hide();
            }
            return;
        }

        /* internal  */removeActive(): void
        {
            this.hintBtn.hide();
            if (!this.view.isHide) 
            {
                this.view.hide();
            }
            return;
        }

        /* internal  */getStatus(arg1: Boolean): void
        {
            GameProxy.roleProxy.holidaySignStatus(arg1);
            return;
        }

        /* internal  */checkBtnShine(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            var loc1=false;
            var loc2=0;
            if (!arg1) 
            {
                return;
            }
            var loc3=0;
            var loc4=arg1;
            for(loc2 of loc4) 
            {
                if (loc2 != ESignStatus._ESignFree) 
                {
                    continue;
                }
                loc1 = true;
                break;
            }
            if (loc1) 
            {
                EffectManager.glowFilterReg(this.hintBtn, [FilterConst.guideTipsFilter]);
            }
            else 
            {
                EffectManager.glowFilterUnReg(this.hintBtn);
            }
            return;
        }

        private /* var */_holidaySignModule: IHolidaySignModule;

        private /* var */_hintBtn: HolidaySignHintBtn;

        private /* var */_activeToClient: SActiveToClient;
    }
